package com.pan.admin.module.loanConfig.controller;

import com.pan.admin.common.annotation.Log;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.domain.AjaxResult;
import com.pan.admin.common.core.page.TableDataInfo;
import com.pan.admin.common.enums.BusinessType;
import com.pan.admin.module.channel.entity.LoanChannelFormField;
import com.pan.admin.module.loanConfig.entity.LoanFormField;
import com.pan.admin.module.loanConfig.service.LoanFormFieldService;
import com.pan.admin.module.loanProduct.entity.LoanProductPlanField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 动态表单字段Controller
 *
 * @author ghb
 * @date 2023-09-11
 */
@RestController
@RequestMapping("/loanFrom/field")
public class LoanFormFieldController extends BaseController {
    @Autowired
    private LoanFormFieldService loanFormFieldService;

    /**
     * 查询动态表单字段列表
     */
    @PreAuthorize("@ss.hasPermi('loanFrom:field:list')")
    @GetMapping("/list")
    public TableDataInfo list(LoanFormField loanFormField) {
        startPage();
        List<LoanFormField> list = loanFormFieldService.list(loanFormField);
        return getDataTable(list);
    }

//    /**
//     * 导出动态表单字段列表
//     */
//    @PreAuthorize("@ss.hasPermi('loanFrom:field:export')")
//    @Log(title = "动态表单字段", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, LoanFormField loanFormField) {
//        List<LoanFormField> list = loanFormFieldService.list(loanFormField);
//        ExcelUtil<LoanFormField> util = new ExcelUtil<LoanFormField>(LoanFormField.class);
//        util.exportExcel(response, list, "动态表单字段数据");
//    }

    /**
     * 获取动态表单字段详细信息
     */
    @PreAuthorize("@ss.hasPermi('loanFrom:field:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(loanFormFieldService.getById(id));
    }

    /**
     * 新增动态表单字段
     */
    @PreAuthorize("@ss.hasPermi('loanFrom:field:add')")
    @Log(title = "动态表单字段", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LoanFormField loanFormField) {
        return toAjax(loanFormFieldService.insertLoanFormField(loanFormField));
    }

    /**
     * 修改动态表单字段
     */
    @PreAuthorize("@ss.hasPermi('loanFrom:field:edit')")
    @Log(title = "动态表单字段", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LoanFormField loanFormField) {
        if (loanFormField.getStatus() == 0) {
            LoanProductPlanField productField = new LoanProductPlanField();
            LoanChannelFormField loanChannelFormField = new LoanChannelFormField();
            Long id = loanFormField.getId();
            int intValue = id.intValue();
            productField.setLoanFormFieldId(intValue);
            loanChannelFormField.setLoanFormFieldId(loanFormField.getId());
            int count = loanFormFieldService.countFormProduct(productField);
            int count1 = loanFormFieldService.countFormChannel(loanChannelFormField);
            if (count > 0) {
                return AjaxResult.success("505", "该字段已关联机构，请取消关联后操作");
            } else if (count1 > 0) {
                return AjaxResult.success("506", "该字段已关联渠道，请取消关联后操作");
            }
        }
        return toAjax(loanFormFieldService.updateLoanFormField(loanFormField));
    }

}
